/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wizard;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Random;
import java.util.Set;
import wizard.FighterPlayer;
import wizard.Player;

/**
 *
 * @author afspear
 */
public class Game {

    private ArrayList<Objects> theObjects = new ArrayList<Objects>();
    private Random random = new Random();
    private WizardPlayer wizard;
    private ArrayList<ScoutPlayer> scouts = new ArrayList<ScoutPlayer>();
    private ArrayList<FighterPlayer> fighters = new ArrayList<FighterPlayer>();
    private ArrayList<Player> allPlayers = new ArrayList<Player>();
    Logger log = new Logger("log.txt");

    public Game() {

        log.logString("\n ***New Game***");
        ArrayList<PlayerName> playerNames = new ArrayList<PlayerName>(Arrays.asList(PlayerName.values()));

        //randomize player names
        Collections.shuffle(playerNames);

        int i = 0;

        for (PlayerName playerName : playerNames) {
            if (i == 0) {
                this.wizard = new WizardPlayer(playerName);
                wizard.setPossessions(new Objects(ObjectType.ring,
                        ObjectPredicate.golden));
                log.logRole(this.wizard.name.toString(), this.wizard.role.toString());
            } else {

                if (random.nextDouble() > .50) {
                    FighterPlayer fighter = new FighterPlayer(playerName);
                    fighter.setRole(PlayerRole.runner);
                    this.fighters.add(fighter);
                    log.logRole(fighter.name.toString(), "Fighter");

                } else {
                    ScoutPlayer scout = new ScoutPlayer(playerName);
                    scout.setRole(PlayerRole.runner);
                    scouts.add(scout);
                    log.logRole(scout.name.toString(), "Scout");
                }

            }
            i++;
        }

        //make a balloon with a random predicate
        Objects balloonObject = new Objects(ObjectType.balloon,
                ObjectPredicate.values()[random.nextInt(ObjectPredicate.values().length)]);
        theObjects.add(balloonObject);

        //make a sword with a random predicate
        Objects swordObject = new Objects(ObjectType.sword,
                ObjectPredicate.values()[random.nextInt(ObjectPredicate.values().length)]);
        theObjects.add(swordObject);

        //the wizard hides all the objects
        for (Objects object : theObjects) {
            this.wizard.hideObject(object);
        }

        this.allPlayers.addAll(scouts);
        this.allPlayers.addAll(fighters);
        this.allPlayers.add(wizard);

        Collections.shuffle(this.allPlayers);
    }

    public void playRound() {
        while (!allAreServants()) {
            int allPlayersSize = allPlayers.size();
            for (int i = 0; i < allPlayersSize - 1; i++) {
                Player player1 = allPlayers.get(i);
                //take that palyer out of the running
                allPlayers.remove(player1);
                //get the 2nd player
                Player player2 = allPlayers.get(random.nextInt(allPlayers.size() - 1));
                //add that player1 back in to the 
                boolean add = allPlayers.add(player1);
                Objects object = theObjects.get(random.nextInt(theObjects.size() - 1));
                playerInteraction(player1, player2, object);
            }
        }
        for (Player player : allPlayers) {
            log.logString(player.name.toString() + " is a " + player.role.toString());
        }

    }

    private void playerInteraction(Player player1, Player player2, Objects object) {


        if (player1.getTired() >= player2.getTired()) {
            player1.tag(player2);
        }


        if (!object.isFound()) {
            player1.find(object);
        }

    }

    public boolean allAreServants() {
        boolean ans = true;


        for (Player player : allPlayers) {
            if (player.getRole() == PlayerRole.runner) {
                ans = false;
                log.logString("\t" + player.getName().toString() + " is a " + player.getRole().toString());
            }
        }
        return ans;
    }
}
